package cn.nexd.app.indoor.ui.activity;

import android.Manifest;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.OnClick;
import cn.nexd.app.indoor.R;
import cn.nexd.app.indoor.perimission.LoadingActivitypermissionsDispatcher;
import cn.nexd.app.indoor.presenter.LoadingPresenter;
import cn.nexd.app.indoor.ui.iView.ILoadingActivityView;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;

/**
 * Created by wangxu on 16/7/19.
 */
public class LoadingActivity extends BaseActivity<LoadingPresenter> implements Animator.AnimatorListener, ILoadingActivityView {
    //2016.3.15 loading页面加动画 加跳过动画textview
    @Bind(R.id.loading_page_imv)
    ImageView mLoadingImg;
    @Bind(R.id.loading_page_skip)
    TextView mLoadingSkip;
    @Bind(R.id.loading_page_start)
    Button mLoadingStart;

    ObjectAnimator anim;
    SharedPreferences spIsFirstInstall;

    @Override
    public int getLayoutRes() {
        //不显示程序的标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //不显示系统的标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.loading_page;
    }

    @Override
    public void initPresenter() {
        presenter = new LoadingPresenter(this, this);
        presenter.init();
    }

    @NeedsPermission(Manifest.permission_group.LOCATION)
    public void locationCurrentPermission() {
        LoadingActivitypermissionsDispatcher.showStorgeWithCheck(this);
    }

    @TargetApi(Build.VERSION_CODES.M)
    @NeedsPermission(Manifest.permission_group.PHONE)
    public void uploadOperaRecord() {
    }

    @NeedsPermission(Manifest.permission_group.STORAGE)
    public void checkPhonePermission() {
        LoadingActivitypermissionsDispatcher.showPhoneWithCheck(this);
    }

    /**
     * 关于存储卡的权限请求
     */
    @OnPermissionDenied(Manifest.permission_group.STORAGE)
    public void onStorageDenied() {
        // NOTE: Deal with a denied permission, e.g. by showing specific UI
        // or disabling certain functionality
        showToast(getString(R.string.permission_sdcard_denied));
    }

    @OnNeverAskAgain(Manifest.permission_group.STORAGE)
    public void onStorageNeverAskAgain() {
        showToast(getString(R.string.permission_camera_never_askagain));
    }

    @OnShowRationale(Manifest.permission_group.STORAGE)
    public void showRationaleForStorage(PermissionRequest request) {
        // NOTE: Show a rationale to explain why the permission is needed, e.g. with a dialog.
        // Call proceed() or cancel() on the provided PermissionRequest to continue or abort
        showRationaleDialog(R.string.permission_sdcard_rationale, request);
    }

    @OnPermissionDenied(Manifest.permission_group.PHONE)
    public void onPhoneDenied() {
        showToast(getString(R.string.permission_phone_denied));
    }

    @OnNeverAskAgain(Manifest.permission_group.PHONE)
    public void onPhoneNeverAskAgain() {
        showToast(getString(R.string.permission_phone_never_askagain));
    }

    @OnShowRationale(Manifest.permission_group.PHONE)
    public void showRationaleForPhone(PermissionRequest permissionRequest) {
        showRationaleDialog(R.string.permission_phone_rationale, permissionRequest);
    }

    /**
     * 关于定位的权限请求
     */
    @OnPermissionDenied(Manifest.permission_group.LOCATION)
    public void onLocationDenied() {
        showToast(getString(R.string.permission_location_denied));
    }

    @OnNeverAskAgain(Manifest.permission_group.LOCATION)
    public void onLocationNeverAskAgain() {
        showToast(getString(R.string.permission_location_never_askagain));
    }

    @OnShowRationale(Manifest.permission_group.LOCATION)
    public void showRationaleForLocation(PermissionRequest request) {
        showRationaleDialog(R.string.permission_location_rationale, request);
    }

    @Override
    public void initView() {
        presenter.startAnim();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        LoadingActivitypermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @OnClick({R.id.loading_page_skip, R.id.loading_page_start})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.loading_page_skip:
                anim.cancel();
                mLoadingStart.setVisibility(View.GONE);
                intoOtherActivity();
                break;

            case R.id.loading_page_start:
                intoOtherActivity();
                break;
        }
    }

    private void showRationaleDialog(@StringRes int messageResId, final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .setCancelable(false)
                .setMessage(messageResId)
                .show();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(LoadingActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void initAnimation() {
        startAnimation();
        bindListener();
        LoadingActivitypermissionsDispatcher.showLocationWithCheck(this);
    }

    private void startAnimation() {
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1, 1.5f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1, 1.5f);
        anim = ObjectAnimator.ofPropertyValuesHolder(mLoadingImg, scaleX, scaleY).setDuration(3000);
        anim.start();
    }

    private void bindListener() {
        anim.addListener(this);
    }

    @Override
    protected void onDestroy() {
        if (anim != null) {
            anim.cancel();
        }
        super.onDestroy();
    }

    @Override
    public void intoOtherActivity() {
        Intent intent = new Intent(this, SignInActivity.class);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        mLoadingSkip.setVisibility(View.GONE);
        mLoadingStart.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }
}
